﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MyInformation : System.Web.UI.Page
{

    int userId=1;
    List<Watch> watchItems= new List<Watch>();
    List<Bid> bidItems=new List<Bid>( );
    List<Bid> biddenItems=new List<Bid>();
    List<Bid> bidingItems=new List<Bid>();
    List<Item> saleItems=new List<Item>();
    List<Item> soldItems=new List<Item>();
    List<Item> salingItems=new List<Item>();
    void infoSelect_clickLinkButton(object sender, String s)
    {
        switch (s)
        {
            case "关注":
                {
                    watchItems = InterfaceManager.DataManager.GetAllWatchs(userId);
                    for (int i = 0; i < watchItems.Count; i++)
                    {
                        control_WatchList control_WatchList = Page.LoadControl("control/WatchList.ascx") as control_WatchList;
                        
                        control_WatchList.Name = InterfaceManager.DataManager.GetItem(watchItems[i].itemId).goodName; ;
                        control_WatchList.MaxBidPrice = InterfaceManager.DataManager.GetItem(watchItems[i].itemId).currentPrice.ToString();
                        TimeSpan ts=(InterfaceManager.DataManager.GetItem(watchItems[i].itemId).closingDatetime - DateTime.Now);
                        control_WatchList.LeftTime = ts.Days.ToString()+"天"+ts.Hours.ToString()+"时"+ts.Minutes.ToString()+"分"+ts.Seconds.ToString()+"秒";
                        control_WatchList.Note = watchItems[i].note;
                        control_WatchList.setPicturePath("../images/" + InterfaceManager.DataManager.GetItem(watchItems[i].itemId).picAddress);
                        control_WatchList.Link = "../BidItem.aspx?ItemId=" + watchItems[i].itemId;
                        this.PlaceHolder.Controls.Add(control_WatchList);
                    }
                    control_Blank blank = Page.LoadControl("control/Blank.ascx") as control_Blank;
                    this.PlaceHolder.Controls.Add(blank);
                    break;
                }

            case "正在竞拍":
                {
                    
                    bidItems = InterfaceManager.DataManager.GetAllBids(userId);
                    for (int i = 0; i < bidItems.Count; i++)
                    {
                        if (bidItems[i].state == Bid_State.NEW || bidItems[i].state == Bid_State.FAILD)
                        {
                            bidingItems.Add(bidItems[i]);
                        }
                    }
                    for (int i = 0; i < bidingItems.Count; i++)
                        {
                            BidingItemInfo control_WatchList = Page.LoadControl("control/BidingItemInfo.ascx") as BidingItemInfo;
                            
                            control_WatchList.Name = InterfaceManager.DataManager.GetItem(bidingItems[i].itemId).goodName; ;
                            control_WatchList.MaxBidPrice = InterfaceManager.DataManager.GetItem(bidingItems[i].itemId).currentPrice.ToString();
                            control_WatchList.Link = "../BidItem.aspx?ItemId=" + bidingItems[i].itemId;
                            control_WatchList.MyBidPrice = bidingItems[i].price.ToString();
                            TimeSpan ts = (InterfaceManager.DataManager.GetItem(bidingItems[i].itemId).closingDatetime - DateTime.Now);
                            control_WatchList.LeftTime = ts.Days.ToString() + "天" + ts.Hours.ToString() + "时" + ts.Minutes.ToString() + "分" + ts.Seconds.ToString() + "秒";
 
                            control_WatchList.setPicturePath("../images/"+InterfaceManager.DataManager.GetItem(bidingItems[i].itemId).picAddress);
                            this.PlaceHolder.Controls.Add(control_WatchList);
                        }
                        control_Blank blank = Page.LoadControl("control/Blank.ascx") as control_Blank;
                        this.PlaceHolder.Controls.Add(blank);
                    break;
                }

            case "竞拍记录":
                {

                    bidItems = InterfaceManager.DataManager.GetAllBids(userId);
                    for (int i = 0; i < bidItems.Count; i++)
                    {
                        if (bidItems[i].state == Bid_State.VECTORY || bidItems[i].state == Bid_State.RECEIVED || bidItems[i].state == Bid_State.PAID)
                        {
                            biddenItems.Add(bidItems[i]);
                        }
                    }
                    for (int i = 0; i < biddenItems.Count; i++)
                    {
                        control_BidHistory control_WatchList = Page.LoadControl("control/BidHistory.ascx") as control_BidHistory;
                        control_WatchList.itemId = biddenItems[i].itemId;
                        control_WatchList.Name = InterfaceManager.DataManager.GetItem(biddenItems[i].itemId).goodName; ;
                        control_WatchList.MaxBidPrice = InterfaceManager.DataManager.GetItem(biddenItems[i].itemId).currentPrice.ToString();
                        control_WatchList.Link = "../BidItem.aspx?ItemId=" + biddenItems[i].itemId;
                        control_WatchList.MyBidPrice = biddenItems[i].price.ToString();
                        control_WatchList.FinishTime = InterfaceManager.DataManager.GetItem(biddenItems[i].itemId).closingDatetime.ToString();
                        control_WatchList.setPicturePath("../images/"+InterfaceManager.DataManager.GetItem(biddenItems[i].itemId).picAddress);

                        //买家对卖家的feedback信息显示
                        if (bidItems[i].state != Bid_State.PAID)
                        {
                            control_WatchList.setCommentState(control_BidHistory.CommentState.noComment);
                        }
                        else
                        {
                            if (InterfaceManager.DataManager.GetFeedbackBy(userId, biddenItems[i].itemId, 1).comment == null)
                            {
                                control_WatchList.setCommentState(control_BidHistory.CommentState.beforeComment);///////////
                                control_WatchList.commentButtonClickEvent += new control_BidHistory.commentButtonClickHandler(control_WatchList_commentButtonClickEvent);
                            }
                            else
                            {
                                if (InterfaceManager.DataManager.GetFeedbackBy(userId, biddenItems[i].itemId, 1).comment != null && InterfaceManager.DataManager.GetFeedbackBy(userId, biddenItems[i].itemId, 1).reply_commment == null)
                                {
                                    control_WatchList.setCommentState(control_BidHistory.CommentState.afterCommentBeforReplied);
                                    control_WatchList.setCommentFormScore(InterfaceManager.DataManager.GetFeedbackBy(userId, biddenItems[i].itemId, 1).score);
                                    control_WatchList.setCommentFormCommentText(InterfaceManager.DataManager.GetFeedbackBy(userId, biddenItems[i].itemId, 1).comment);
                                }
                                else
                                    if (InterfaceManager.DataManager.GetFeedbackBy(userId, biddenItems[i].itemId, 1).comment != null && InterfaceManager.DataManager.GetFeedbackBy(userId, biddenItems[i].itemId, 1).reply_commment != null)
                                    {
                                        control_WatchList.setCommentState(control_BidHistory.CommentState.afterReplied);
                                        control_WatchList.setCommentFormScore(InterfaceManager.DataManager.GetFeedbackBy(userId, biddenItems[i].itemId, 1).score);
                                        control_WatchList.setCommentFormCommentText(InterfaceManager.DataManager.GetFeedbackBy(userId, biddenItems[i].itemId, 1).comment);
                                        control_WatchList.setCommentFormReplyText(InterfaceManager.DataManager.GetFeedbackBy(userId, biddenItems[i].itemId, 1).reply_commment);

                                    }
                            }
                        }
                        //卖家对买家的feedback信息显示
                        if (InterfaceManager.DataManager.GetFeedbackBy(userId, biddenItems[i].itemId, 1).comment == null)
                        {
                            control_WatchList.setCommentedState(control_BidHistory.CommentedState.beforeCommented);///////////
                        }
                        else
                        {
                            if (InterfaceManager.DataManager.GetFeedbackBy(userId, biddenItems[i].itemId, 1).comment != null && InterfaceManager.DataManager.GetFeedbackBy(userId, biddenItems[i].itemId, 1).reply_commment == null)
                            {
                                control_WatchList.setCommentedState(control_BidHistory.CommentedState.afterCommentedBeforeReply);
                                control_WatchList.replyButtonClickEvent+=new control_BidHistory.replyButtonClickHandler(control_WatchList_replyButtonClickEvent);
                                control_WatchList.setCommentedFormScore(InterfaceManager.DataManager.GetFeedbackBy(userId, biddenItems[i].itemId, 0).score);
                                control_WatchList.setCommentedCommentText(InterfaceManager.DataManager.GetFeedbackBy(userId, biddenItems[i].itemId, 1).comment);
                            }
                            else
                                if (InterfaceManager.DataManager.GetFeedbackBy(userId, biddenItems[i].itemId, 1).comment != null && InterfaceManager.DataManager.GetFeedbackBy(userId, biddenItems[i].itemId, 1).reply_commment != null)
                                {
                                    control_WatchList.setCommentedState(control_BidHistory.CommentedState.afterReply);
                                    control_WatchList.setCommentedFormScore(InterfaceManager.DataManager.GetFeedbackBy(userId, biddenItems[i].itemId, 0).score);
                                    control_WatchList.setCommentedCommentText(InterfaceManager.DataManager.GetFeedbackBy(userId, biddenItems[i].itemId, 0).comment);
                                    control_WatchList.setCommentedFormReplyText(InterfaceManager.DataManager.GetFeedbackBy(userId, biddenItems[i].itemId, 0).reply_commment);

                                }


                        }
                        this.PlaceHolder.Controls.Add(control_WatchList);
                    }
                    control_Blank blank = Page.LoadControl("control/Blank.ascx") as control_Blank;
                    this.PlaceHolder.Controls.Add(blank);
                    break;
                }

            case "正在拍卖":
                {

                    salingItems = InterfaceManager.DataManager.GetItemByAuctioneer(userId);
                    for (int i = 0; i < saleItems.Count; i++)
                    {
                        if (!saleItems[i].isSold)
                        {
                            salingItems.Add(saleItems[i]);
                        }
                    }
                    for (int i = 0; i < salingItems.Count; i++)
                    {
                        control_SellingItemInfo control_WatchList = Page.LoadControl("control/SellingItemInfo.ascx") as control_SellingItemInfo;

                        control_WatchList.Name = salingItems[i].goodName; ;
                        control_WatchList.MaxBidPrice = salingItems[i].currentPrice.ToString();
                        control_WatchList.ReservedPrice = salingItems[i].reservePrice.ToString();
                        control_WatchList.LeftTime = salingItems[i].closingDatetime.ToString();
                        control_WatchList.Description = salingItems[i].description;
                        //control_WatchList.BidTime=
                        control_WatchList.Link = "../BidItem.aspx?ItemId=" + salingItems[i].id;
                        control_WatchList.setPicturePath("../images/" + salingItems[i].picAddress);
                        this.PlaceHolder.Controls.Add(control_WatchList);
                    }
                    control_Blank blank = Page.LoadControl("control/Blank.ascx") as control_Blank;
                    this.PlaceHolder.Controls.Add(blank);
                    break;
                }

            case "拍卖记录":
                {
                    saleItems = InterfaceManager.DataManager.GetItemByAuctioneer(userId);
                    for (int i = 0; i < saleItems.Count; i++)
                    {
                        if (saleItems[i].isSold)
                        {
                            soldItems.Add(saleItems[i]);
                        }
                    }
                    for (int i = 0; i < soldItems.Count; i++)
                    {
                        control_SellHistory control_WatchList = Page.LoadControl("control/SellHistory.ascx") as control_SellHistory;
                       
                        control_WatchList.Name = soldItems[i].goodName; ;
                        control_WatchList.MaxBidPrice = soldItems[i].currentPrice.ToString();
                        control_WatchList.ReservedPrice = soldItems[i].reservePrice.ToString();
                        control_WatchList.FinishTime = soldItems[i].closingDatetime.ToString();
                        control_WatchList.Result = "已卖出";
                        control_WatchList.setPicturePath("../images/" + soldItems[i].picAddress);
                        control_WatchList.Link = "../BidItem.aspx?ItemId=" + soldItems[i].id;
                        //买家对卖家的feedback信息显示

                        if (InterfaceManager.DataManager.GetFeedbackBy(userId, soldItems[i].id, 1).comment == null)
                        {
                            control_WatchList.setCommentedState(control_SellHistory.CommentedState.beforeCommented);///////////

                        }
                        else
                        {
                            if (InterfaceManager.DataManager.GetFeedbackBy(userId, soldItems[i].id, 1).comment != null && InterfaceManager.DataManager.GetFeedbackBy(userId, soldItems[i].id, 1).reply_commment == null)
                            {
                                control_WatchList.setCommentedState(control_SellHistory.CommentedState.afterCommentedBeforeReply);
                                control_WatchList.replyButtonClickEvent+=new control_SellHistory.replyButtonClickHandler(control_WatchList_repliedButtonClickEvent);
                                control_WatchList.setCommentFormScore(InterfaceManager.DataManager.GetFeedbackBy(userId, soldItems[i].id, 1).score);
                                control_WatchList.setCommentedCommentText(InterfaceManager.DataManager.GetFeedbackBy(userId, soldItems[i].id, 1).comment);
                            }
                            else
                                if (InterfaceManager.DataManager.GetFeedbackBy(userId, soldItems[i].id, 1).comment != null && InterfaceManager.DataManager.GetFeedbackBy(userId, soldItems[i].id, 1).reply_commment != null)
                                {
                                    control_WatchList.setCommentedState(control_SellHistory.CommentedState.afterReply);
                                    control_WatchList.setCommentedFormScore(InterfaceManager.DataManager.GetFeedbackBy(userId, soldItems[i].id, 1).score);
                                    control_WatchList.setCommentedCommentText(InterfaceManager.DataManager.GetFeedbackBy(userId, soldItems[i].id, 1).comment);
                                    control_WatchList.setCommentedFormReplyText(InterfaceManager.DataManager.GetFeedbackBy(userId, soldItems[i].id, 1).reply_commment);

                                }
                        }
                        
                        //卖家对买家的feedback信息显示
                        if (InterfaceManager.DataManager.GetFeedbackBy(userId, soldItems[i].id, 0).comment == null)
                        {
                            control_WatchList.setCommentState(control_SellHistory.CommentState.beforeComment);///////////
                            control_WatchList.commentButtonClickEvent += new control_SellHistory.commentButtonClickHandler(control_WatchList_commentedButtonClickEvent);
                        }
                        else
                        {
                            if (InterfaceManager.DataManager.GetFeedbackBy(userId, soldItems[i].id, 0).comment != null && InterfaceManager.DataManager.GetFeedbackBy(userId, soldItems[i].id, 0).reply_commment == null)
                            {
                                control_WatchList.setCommentState(control_SellHistory.CommentState.afterCommentBeforReplied);
                                control_WatchList.setCommentFormScore(InterfaceManager.DataManager.GetFeedbackBy(userId, soldItems[i].id, 0).score);
                                control_WatchList.setCommentFormCommentText(InterfaceManager.DataManager.GetFeedbackBy(userId, soldItems[i].id, 0).comment);
                            }
                            else
                                if (InterfaceManager.DataManager.GetFeedbackBy(userId, soldItems[i].id, 0).comment != null && InterfaceManager.DataManager.GetFeedbackBy(userId, soldItems[i].id, 0).reply_commment != null)
                                {
                                    control_WatchList.setCommentState(control_SellHistory.CommentState.afterReplied);
                                    control_WatchList.setCommentFormScore(InterfaceManager.DataManager.GetFeedbackBy(userId, soldItems[i].id, 0).score);
                                    control_WatchList.setCommentFormCommentText(InterfaceManager.DataManager.GetFeedbackBy(userId, soldItems[i].id, 0).comment);
                                    control_WatchList.setCommentFormReplyText(InterfaceManager.DataManager.GetFeedbackBy(userId, soldItems[i].id, 0).reply_commment);
                                }
                        }
                        this.PlaceHolder.Controls.Add(control_WatchList);
                    }
                    control_Blank blank = Page.LoadControl("control/Blank.ascx") as control_Blank;
                    this.PlaceHolder.Controls.Add(blank);
                    break;
                }

        }
    }
    //竞拍记录的评论按钮
    void control_WatchList_commentButtonClickEvent(object sender, EventArgs e)
    {
        control_BidHistory cb = (control_BidHistory)sender;
        Feedback fb = new Feedback(cb.itemId, userId, cb.getCommentFormScore(), cb.getComment(), 1);
        
        InterfaceManager.DataManager.AddFeedback(fb);
    }
    //竞拍记录回复按钮
    void control_WatchList_replyButtonClickEvent(object sender, EventArgs e)
    {
        control_BidHistory cb = (control_BidHistory)sender;
        Feedback fb = InterfaceManager.DataManager.GetFeedbackBy(userId, cb.itemId, 1);
        fb.reply_commment = cb.getReply();
        InterfaceManager.DataManager.AddReply(fb);
    }

    //拍卖记录的评论按钮
    void control_WatchList_commentedButtonClickEvent(object sender, EventArgs e)
    {
        control_BidHistory cb = (control_BidHistory)sender;
        Feedback fb = new Feedback(cb.itemId, userId, cb.getCommentFormScore(), cb.getComment(), 0);

        InterfaceManager.DataManager.AddFeedback(fb);
    }

    //拍卖纪录回复按钮
    void control_WatchList_repliedButtonClickEvent(object sender, EventArgs e)
    {
        control_BidHistory cb = (control_BidHistory)sender;
        Feedback fb = InterfaceManager.DataManager.GetFeedbackBy(userId, cb.itemId, 0);
        fb.reply_commment = cb.getReply();
        InterfaceManager.DataManager.AddReply(fb);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        userId = Convert.ToInt32(Session["usrId"]);
        User user=InterfaceManager.DataManager.GetUsrBasicInfo(userId);
        myInformation.Name=user.userName;
        myInformation.Email=user.email;
        myInformation.Phone = user.telephone;
        myInformation.Address = user.address;
        this.infoSelect.clickLinkButton+=new Select.LinkButtonClickHandler(infoSelect_clickLinkButton);

       
       
    }
}